package com.jt.controller;

import com.jt.pojo.User;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class AxiosController {
    @GetMapping("/axios/getUser")
    public String getUser(){
        return "您好ajax入门";
    }
    /**
     * 查询用户信息
     * URL: http://localhost:8090/axios/getUserById?id=100
     * 参数: id=100
     * 返回值: String
     */
    @GetMapping("/axios/getUserById")
    public String getUserById(Integer id){

        return "axios的ID查询:"+id;
    }
    /**
     * restFul ajax传参
     * URL: http://localhost:8090/axios/user/mysql/20
     * 参数: name/age
     * 返回值: User对象
     */
    @GetMapping("/axios/user/{name}/{age}")
    public User getUser2(User user){
        return user;
    }
    /**
     * url:http://localhost:8090/axios/user/getUserObj
     * 参数:多个参数使用对象接收
     * 返回值:User
     * */
    @GetMapping("/axios/user/getUserObj")
    public  User getUserObj(User user){
        return user;
    }
    /**
     *  需求:post请求实现数据入库
     *  URL: http://localhost:8090/axios/insertUser
     *  参数: "user对象结构"
     *  返回值: User对象返回
     *  注意事项:
     *      如果前端发起post请求方式,则传递的数据是一个JSON串
     *      常规参数: id=100 name="tomcat"
     *      post参数: {id:100,name:"tomcat"}
     *  解决方案:
     *       1.对象转化为JSON     @ResponseBody
     *       2.JSON串转化为对象   @RequestBody
     */
    @PostMapping("/axios/insertUser")
    public User insertUser(@RequestBody User user){
        return user;
    }
    /**
     * url:http://localhost:8090/axios/user/name/age/sex
     * 参数: 对象接收
     * 返回值: User对象  demo练习简单 由业务决定
     */
    @PostMapping("/axios/user/{name}/{age}/{sex}")
    public User insertUserRest(User user){
        System.out.println("调用service完成入库");
        return user;
    }
}
